package com.shaoshaohuo.app.adapter;

import java.util.List;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.constant.ExtraName;
import com.shaoshaohuo.app.entity.BusinessBuyer;
import com.shaoshaohuo.app.entity.Buyer;
import com.shaoshaohuo.app.entity.PurchaseDetail.ProductMore;
import com.shaoshaohuo.app.ui.ec.MarketDetailActivity;
import com.shaoshaohuo.app.ui.ec.MoreBuyerActivity;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class MarketListAdapter<T> extends MyBaseAdapter<BusinessBuyer> {

	public MarketListAdapter(Context ctx, List<BusinessBuyer> list, boolean flag) {
		super(ctx, list, flag);
	}

	@Override
	public View getView(int pos, View convertView, ViewGroup group) {
		
		ViewHolder holder;
        if (convertView == null) {
            convertView = View.inflate(context,R.layout.view_buyers_list_item, null);
            holder = new ViewHolder();
            holder.titleText = (TextView) convertView.findViewById(R.id.textview_market_name);
            holder.mListview = (ListView) convertView.findViewById(R.id.listview_data_list);
            holder.mGridview = (GridView) convertView.findViewById(R.id.gv_service_list);
            holder.moreText = (TextView) convertView.findViewById(R.id.textview_more);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
		
        final BusinessBuyer item = list.get(pos);
        
		MerchantsListAdapter<Buyer> adapter = new MerchantsListAdapter<>(context, item.getBuyers(), false);
		holder.mListview.setAdapter(adapter);
		
		ProductGridviewAdapter<ProductMore> adapter1 = new ProductGridviewAdapter<>(context, item.getCategorys(), false);
		holder.mGridview.setAdapter(adapter1);
        
		holder.mListview.setClickable(false);
		holder.mGridview.setClickable(false);
		
        holder.titleText.setText(item.getMarketname());
        
        holder.mListview.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(context,MarketDetailActivity.class);
                intent.putExtra(ExtraName.id, item.getMarketid());
                intent.putExtra(ExtraName.marketName, item.getMarketname());
                context.startActivity(intent);
            }
        });;
        holder.mGridview.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(context,MarketDetailActivity.class);
                intent.putExtra(ExtraName.id, item.getMarketid());
                intent.putExtra(ExtraName.marketName, item.getMarketname());
                context.startActivity(intent);
            }
        });
        
		holder.moreText.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
			    Intent intent = new Intent(context,MoreBuyerActivity.class);
			    intent.putExtra(ExtraName.id, item.getMarketid());
			    context.startActivity(intent);
			}
		});
		
		return convertView;
	}
	
	static class ViewHolder{
		TextView titleText;
		
		ListView mListview;
		GridView mGridview;
		
		TextView moreText;
	}
	
}
